var express = require('express');
var router = express.Router();
var connection = require('../db/sql.js')
var user = require('../db/userSql.js')
/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', {
    title: 'Express'
  });
});
//添加用户
router.post('/api/addUser', function (req, res, next) {
  let params = {
    username: req.body.username,
    password: req.body.password,
    email: req.body.email,
    mobile: req.body.mobile
  }
  
  //查询用户是否存在
  connection.query(user.queryUserName(params), function (error, results) {
    if (error) throw error;
    //用户存在
    if (results.length > 0) {
      res.send({
        code: 302,
        data: {
          success: false,
          msg: '用户名已存在',
        }
      })
    } else {
      //不存在，新增一条数据
      connection.query(user.inserData(params), function (err, result) {
        connection.query(user.queryUserName(params), function (e, r) {
          
          res.send({
            code: 200,
            data: {
              success: true,
              msg: '登录成功',
              data: r[0]
            }
          })
        })
      })
    }
  })
})
//用户列表
router.get('/api/user', function (req, res, next) {
  let [userName] = Object.values(req.query)
  connection.query('select * from user where userName like "%'+userName+'%"', function (error, results) {
    res.send({
      code: 200,
      data:[{
        pagenum:1,
        total:15,
        data: results
      }]
    })
  })
});
// 左侧菜单接口
router.get('/api/menus', function (req, res, next) {
  res.send({
    code: 200,
    data: [{
        id: 1,
        authName: '用户管理',
        path: 'users',
        children: [{
          id: 1,
          authName: '用户列表',
          order:null,
          path:'users'
        }],
        order: 1
      },
      {
        id: 2,
        authName: '权限管理',
        path: 'rights',
        children: [{
            id: 1,
            authName: '角色列表',
            order:null,
            path: 'roles'
          },
          {
            id: 2,
            authName: '权限列表',
            order: null,
            path: 'rights'
          },
        ],
        order: 2
      },
      {
        id: 3,
        authName: '商品管理',
        path: 'goods',
        children: [{
            id: 1,
            authName: '商品列表',
            order:null,
            path: 'goods'
          },
          {
            id: 2,
            authName: '分类参数',
            order: null,
            path: 'params'
          },
          {
            id: 3,
            authName: '商品分类',
            order: null,
            path: 'categories'
          },
        ],
        order: 3
      },
      {
        id: 4,
        authName: '订单管理',
        path: 'orders',
        children: [{
          id: 1,
          authName: '订单列表',
          order:null,
          path: 'orders'
        }, ],
        order: 4
      },
    ]
  })
});
// 登录接口
router.post('/api/login', function (req, res, next) {
  // 接收前端传来的数据
  let params = {
    username: req.body.username,
    password: req.body.password
  }
  // 查询手机号是否存在
  connection.query(user.queryUserName(params), function (error, results, fields) {
    // 手机号存在
    if (results.length > 0) {
      connection.query(user.queryUserPwd(params), function (err, result) {
        // 密码正确
        if (result.length > 0) {
          res.send({
            code: 200,
            data: {
              success: true,
              msg: '登录成功',
              data: result[0]
            }
          })
        } else {
          // 密码不正确
          res.send({
            code: 301,
            data: {
              success: false,
              msg: '密码不正确'
            }
          })
        }
      })
    } else {
      // 不存在
      res.send({
        code: 301,
        data: {
          success: false,
          msg: '用户名不存在'
        }
      })
    }
  })


})
module.exports = router;